<template>
  <div class="detailPage">
    <!--轮播图 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="#1989fa">
      <van-swipe-item v-for="item in banner" :key="item.id">
        <img :src="item.img_url" alt="" width="100%" />
      </van-swipe-item>
    </van-swipe>

    <!-- Tips组件 -->
    <Tips />
    <!-- 产品信息渲染和展示弹出层 -->
    <div class="info">
      <h3>{{ info.name }}</h3>
      <p>{{ info.goods_brief }}</p>
      <div>{{ info.retail_price | RMBFormat }}</div>
    </div>
    <van-cell title="展示弹出层" is-link @click="isShowSku = true" />
    <!-- 商品参数 -->
    <div class="attribute">
      <h4>商品参数</h4>
      <ul>
        <li v-for="(item, index) in attribute" :key="index">
          <span>{{ item.name }}</span>
          <section class="van-ellipsis">{{ item.value }}</section>
        </li>
      </ul>
    </div>
    <!-- 产品详情图片 -->
    <div class="goods_desc" v-html="info.goods_desc"></div>
    <!-- 常见问题 -->
    <div class="title">
      <span>常见问题</span>
    </div>
    <ul class="issue">
      <li v-for="item in issue" :key="item.id">
        <h3>{{ item.question }}</h3>
        <p>{{ item.answer }}</p>
      </li>
    </ul>
    <!-- 相关产品 -->
    <div class="title">
      <span>相关产品</span>
    </div>
    <Products :productsListData="goodsList" />

    <!-- 商品导航 -->
    <AppGoodsAction @showSku="addToCart" :goodsCount="goodsCount" />

    <!-- sku电商购物栏 -->
    <van-sku
      ref="sku"
      v-model="isShowSku"
      :sku="sku"
      :goods="goods"
      :hide-stock="sku.hide_stock"
    />
  </div>
</template>

<script>
import Tips from "@/components/Tips";
import AppGoodsAction from "../components/AppGoodsAction.vue";
import Products from "@/components/Products";
import {
  GetGoodsDetail,
  GetGoodsRelated,
  GetCartNum,
  ToAddCart,
} from "../request/api";
export default {
  components: {
    Tips,
    Products,
    AppGoodsAction,
  },
  data() {
    return {
      // sku购物车数据
      isShowSku: false,
      sku: {
        //tree : 可以理解为一个商品可以多个规格类目,一个规格类目下可以有多个规格值
        tree: [],
        price: "100.00", // 默认价格（单位元）
        stock_num: 227, // 商品总库存
        hide_stock: false, // 是否隐藏库存
      },
      goods: {
        // 默认商品 sku 缩略图  "" 是图片路径
        picture: "",
      },
      // 购物车数量
      goodsCount: 0,
      // 以下为详情页展示数据
      banner: [],
      info: [],
      attribute: [],
      issue: [],
      // 相关产品的数组
      goodsList: [],
      //productList 用来传值
      productList: [],
    };
  },
  methods: {
    //点击加入购物车 业务逻辑
    addToCart() {
      //点击假如购物车,执行这里的代码
      //判断如果没有登录,也要跳转到登录页面,提示请先登录

      /* 用组件调用了 所以这里可以注释掉了 
      let token = localStorage.getItem("token");
      if (!token) {
        this.$toast("请先登录!");
        setTimeout(() => {
          this.$router.push("/user");
        }, 1000);
        return;
      } */

      //如果直接调用使用的话 utils需要引入到main.js全局里去使用
      let ret = this.utils.mustBelogin();
      if (!ret) return;

      //如果sku是展开的,就执行真正的加购物车操作
      if (this.isShowSku) {
        // console.log("拿到这个num的数量：", this.$refs.sku.getSkuData().selectedNum);
        let stepNum = this.$refs.sku.getSkuData().selectedNum;
        //真正执行发起假如购物车的请求
        ToAddCart({
          goodsId: this.$route.query.id,
          //在请求数据里 Previvw 查看
          productId: this.productList[0].id,
          number: stepNum,
        }).then((res) => {
          // console.log("点击加入购物车之后的res",res); //这里有问题

          // 更新购物车
          this.goodsCount = res.data.cartTotal.goodsCount;
          //提示加入成功
          this.$toast.success("添加成功");
        });
      }
      //显示隐藏加入购物车按钮
      this.isShowSku = !this.isShowSku;
    },
  },
  created() {
    GetGoodsDetail({
      id: this.$route.query.id,
    }).then((res) => {
      // 商品详情信息
      let { gallery, info, attribute, issue, productList } = res.data;
      this.banner = gallery;
      this.info = info;
      this.attribute = attribute;
      this.issue = issue;
      // sku中的商品数据
      this.goods.picture = info.list_pic_url;
      this.sku.price = info.retail_price;
      this.sku.stock_num = info.goods_number;
      this.productList = productList;
    });
    //相关产品的请求
    GetGoodsRelated({
      id: this.$route.query.id,
    }).then((res) => {
      //   console.log( res.data.goodsList);//相关产品
      let { goodsList } = res.data;
      this.goodsList = goodsList;
    });

    //获取购物车数量的请求
    GetCartNum().then((res) => {
      this.goodsCount = res.data.cartTotal.goodsCount;
    });
  },
};
</script>
 
<style lang = "less" scoped>
.detailPage {
  padding-bottom: 0.5rem;
}
.info {
  background: #fff;
  text-align: center;
  padding: 0.2rem 0;
  h3 {
    font-weight: normal;
    font-size: 0.2rem;
    line-height: 0.5rem;
  }
  p {
    color: #999;
    font-size: 0.14rem;
    line-height: 0.3rem;
  }
  div {
    color: darkred;
    font-size: 0.14rem;
    line-height: 0.3rem;
  }
}
.attribute {
  margin-top: 0.2rem;
  background: #fff;
  padding: 0.2rem 4%;
  h4 {
    font-weight: normal;
    font-size: 0.16rem;
    margin-bottom: 0.2rem;
  }
  ul {
    li {
      background: #efefef;
      line-height: 0.4rem;
      height: 0.4rem;
      margin-bottom: 0.1rem;
      border-radius: 4px;
      display: flex;
      justify-content: space-between;
      span {
        width: 30%;
        text-align: right;
      }
      section {
        flex: 1;
        padding-left: 0.2rem;
      }
    }
  }
}
/* 如果样式无法替换，可以试试/deep/ 深度样式写法 */
/deep/.goods_desc {
  img {
    max-width: 100%;
    display: block;
  }
}
.title {
  width: 100%;
  background: #fff;
  height: 0.5rem;
  position: relative;
  &::before {
    content: "";
    width: 50%;
    height: 2px;
    background: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -1px;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    position: relative;
    background: #fff;
    display: block;
    text-align: center;
    margin: 0 auto;
    height: 0.5rem;
    line-height: 0.5rem;
  }
}
.issue {
  padding: 0 4%;
  background: #fff;
  li {
    h3 {
      height: 0.3rem;
      line-height: 0.3rem;
      padding-left: 0.2rem;
      position: relative;
      font-weight: normal;
      &::before {
        content: "";
        width: 4px;
        height: 4px;
        background: darkred;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 50%;
        margin-top: -2px;
      }
    }
    p {
      font-size: 0.12rem;
      line-height: 0.2rem;
      padding-left: 0.2rem;
    }
  }
}
/deep/.van-sku-messages {
  padding: 0;
}
.van-sku-container {
  min-height: auto;
}
/deep/.van-sku__stepper-title,
/deep/.van-sku__stepper {
  float: none;
}
</style>